import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store"
import Main from "@/views/Main";
import Login from "@/views/Login";
import Service from "@/views/Service";
import Identify from "@/views/user/Identify";
import Center from "@/views/user/Center";
import FarmerMain from "@/views/farmer/FarmerMain";
import DiseasesSelect from "@/views/user/DiseasesSelect";
import ExpertSelect from "@/views/user/ExpertSelect";
import Question from "@/views/farmer/Question";
import ChangeFarmerInfo from "@/views/farmer/ChangeFarmerInfo";
import FarmerList from "@/views/admin/FarmerList";
import ExpertList from "@/views/admin/ExpertList";
import CheckExpert from "@/views/admin/CheckExpert";
import ChangeExpertInfo from "@/views/expert/ChangeExpertInfo";
import UnsolvedQuestionList from "@/views/expert/UnsolvedQuestionList";
import SolvedQuestionList from "@/views/expert/SolvedQuestionList";

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const rooter = new VueRouter({
  mode:'history',
  routes:[
    {
      path:'/',
      name:'main',
      component:Main,
      redirect:{name:'service'},
      children:[
        {
          path:'service',
          name:'service',
          component:Service,
          meta: {
            showTitle:"农作物病害识别系统"
          }
        },
        {
          path:'identify',
          name:'identify',
          component:Identify,
          meta: {
            showTitle:"农作物病害识别"
          }
        },
        {
          path:'diseasesSelect',
          name:'diseasesSelect',
          component:DiseasesSelect,
          meta: {
            showTitle:"农作物病害查询"
          }
        },
        {
          path:'expertSelect',
          name:'expertSelect',
          component:ExpertSelect,
          meta: {
            showTitle:"专家查询"
          }
        },
      ]
    },
    {
      path:'/center',
      name:'center',
      component:Center,
      meta:{
        requireAuth:true
      },
      redirect:{name:"farmerMain"},
      children:[
        {
          path: 'farmerMain',
          name: 'farmerMain',
          component: FarmerMain,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'question',
          name:'question',
          component:Question,
          meta: {
            requireAuth:true
          }
        },
        {
          path: "farmerInfo",
          name:"farmerInfo",
          component: ChangeFarmerInfo,
          meta: {
            requireAuth: true
          }
        },
        {
          path: "farmerList",
          name:"farmerList",
          component: FarmerList,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'expertList',
          name: 'expertList',
          component: ExpertList,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'checkExpert',
          name: 'checkExpert',
          component: CheckExpert,
          meta: {
            requireAuth: true
          }
        },
        {
          path: "expertInfo",
          name:"expertInfo",
          component: ChangeExpertInfo,
          meta: {
            requireAuth: true
          }
        },
        {
          path: "unsolved",
          name:"unsolved",
          component: UnsolvedQuestionList,
          meta: {
            requireAuth: true
          }
        },
        {
          path: "solved",
          name:"solved",
          component: SolvedQuestionList,
          meta: {
            requireAuth: true
          }
        },
      ]
    },
    {
      path:'/login',
      name:'login',
      component:Login,
      meta:{
        showHeader:true,
        showFooter:true,
      }
    }
  ],
})


//路由守卫
rooter.beforeEach((to,from,next)=>{
  if (to.name === 'Login') {
    next();
    return;
  }

  if (!store.state.user.loginUser.isLogin) {
    if (to.meta.requireAuth || to.name == null) {
      //将跳转的路由path作为参数，登录成功后跳转到该路由
      next({name: 'login'})
    } else {
      next();
    }
  }else {
    next();
  }
})

export default rooter